
# Code for Table R1: The Effect of Pretrial Incarceration by Race and Prior Registration

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------

library('AER')
library('ivpack')
library('dplyr')
library('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812 <- constructIV(data0812)

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)

## ---------------------------------------
## Omit defendants too young to vote in 2008
## ---------------------------------------
data0812 <- data0812[noteli08 == 0, ]

# convert weighted measure of pretreatment registration into binary
data0812$regis_before <- as.numeric(data0812$regis_before > 0.80)

## ---------------------------------------
## Two Stage Least Squares Specifications
## ---------------------------------------

## ---------------------------------------
## 1. Main Specifications
## ---------------------------------------
time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)"
case.controls <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
demo.controls <- "age_2012 + I(age_2012^2) + Female + vote2008"

outc.1 <- "vote2012"
endo.1 <- "pti * as.factor(regis_before) * as.factor(race)"
inst.1 <- "judgeiv * as.factor(regis_before) * as.factor(race)"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "|", inst.1, "+", time.controls, "+", demo.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))

m1a1 <- ivreg(form.1, data = data0812)
m1a2 <- ivreg(form.2, data = data0812)
m1a3 <- ivreg(form.3, data = data0812)

m1a1_d1 <- summary(m1a1, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti)",3]
m1a2_d1 <- summary(m1a2, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti)",3]
m1a3_d1 <- summary(m1a3, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti)",3]

m1a1_d22 <- summary(m1a1, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(race)White)",3]
m1a2_d22 <- summary(m1a2, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(race)White)",3]
m1a3_d22<- summary(m1a3, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(race)White)",3]


m1a1_d2ii <- summary(m1a1, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(regis_before)1)",3]
m1a2_d2ii <- summary(m1a2, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(regis_before)1)",3]
m1a3_d2ii <- summary(m1a3, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(regis_before)1)",3]

m1a1_d2i <- summary(m1a1, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(regis_before)1:as.factor(race)White)",3]
m1a2_d2i <- summary(m1a2, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(regis_before)1:as.factor(race)White)",3]
m1a3_d2i <- summary(m1a3, vcov = sandwich, diagnostic = TRUE)$diagnostics["Weak instruments (pti:as.factor(regis_before)1:as.factor(race)White)",3]



row1 <- data.table(round(rbind(# estimates
  c(robust.se(m1a1)[c("pti"), c(1)], robust.se(m1a2)[c("pti"), c(1)],  robust.se(m1a3)[c("pti"), c(1)]),
  # standard errors
  c(robust.se(m1a1)[c("pti"), c(2)], robust.se(m1a2)[c("pti"), c(2)], robust.se(m1a3)[c("pti"), c(2)]),
  # f statistics
  c(m1a1_d1, m1a2_d1, m1a3_d1),
  
  # estimates
  c(robust.se(m1a1)[c( "pti:as.factor(regis_before)1"), c(1)], 
    robust.se(m1a2)[c("pti:as.factor(regis_before)1"), c(1)], 
    robust.se(m1a3)[c("pti:as.factor(regis_before)1"), c(1)]),
  # standard errors
  c(robust.se(m1a1)[c("pti:as.factor(regis_before)1"), c(2)], 
    robust.se(m1a2)[c("pti:as.factor(regis_before)1"), c(2)], 
    robust.se(m1a3)[c("pti:as.factor(regis_before)1"), c(2)]),
  # f statistics
  c(m1a1_d2ii, m1a2_d2ii, m1a3_d2ii),
  
  # estimates
  c(robust.se(m1a1)[c( "pti:as.factor(race)White"), c(1)], 
    robust.se(m1a2)[c("pti:as.factor(race)White"), c(1)], 
    robust.se(m1a3)[c("pti:as.factor(race)White"), c(1)]),
  # standard errors
  c(robust.se(m1a1)[c("pti:as.factor(race)White"), c(2)], 
    robust.se(m1a2)[c("pti:as.factor(race)White"), c(2)], 
    robust.se(m1a3)[c("pti:as.factor(race)White"), c(2)]),
  # f statistics
  c(m1a1_d22, m1a2_d22, m1a3_d22),
  
  # estimates
  c(robust.se(m1a1)[c("pti:as.factor(regis_before)1:as.factor(race)White"), c(1)], 
    robust.se(m1a2)[c("pti:as.factor(regis_before)1:as.factor(race)White"), c(1)], 
    robust.se(m1a3)[c("pti:as.factor(regis_before)1:as.factor(race)White"), c(1)]),
  # standard errors
  c(robust.se(m1a1)[c("pti:as.factor(regis_before)1:as.factor(race)White"), c(2)], 
    robust.se(m1a2)[c("pti:as.factor(regis_before)1:as.factor(race)White"), c(2)], 
    robust.se(m1a3)[c("pti:as.factor(regis_before)1:as.factor(race)White"), c(2)]),
  # f statistics
  c(m1a1_d2i, m1a2_d2i, m1a3_d2i),
  # obs
  c(m1a1$n, m1a2$n, m1a3$n),
  rep(NA, 3)
),3))


cont <- row1
colnames(cont) <- c("(1)", "(2)", "(3)")
# add row labels
cont$names <-  c("PTI",
                 "Std. Error",
                 "F-test", 
                 "PTI × Registered Voter",
                 "Std. Error",
                 "F-test", 
                 "PTI X White",
                 "Std. Error",
                 "F-test", 
                 "PTI X Registered Voter X White",
                 "Std. Error",
                 "F-test", 
                 "Obs.",
                 "")
cont <- cont[, c("names", "(1)", "(2)", "(3)")]
cat("\nPrinting Table R1: The Effect of Pretrial Incarceration by Race and Prior Registration...\n")
print(cont)


